package com.baikeyang.service.user.impl;

import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.User;
import com.baikeyang.common.model.UserRole;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.common.util.shiro.SecurityUtil;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.mapper.UserMapper;
import com.baikeyang.mapper.UserRoleMapper;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baikeyang.service.user.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created by lenovo on 2018/7/12.
 */
@Service(value = "userService")
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public User getUserByLoginName(String loginName) {
        QueryWrapper<User> userQueryWrapper = Wrappers.query();
        userQueryWrapper.eq("login_name", loginName);
        List<User> users = userMapper.selectList(userQueryWrapper);
        if(!CollectionUtils.isEmpty(users)){
            return users.get(0);
        }
        return null;
    }

    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectById(id);
        QueryWrapper<UserRole> queryWrapper = Wrappers.query();
        queryWrapper.eq("user_id", user.getId());
        List<UserRole> userRoles = userRoleMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(userRoles)){
            user.setRole(userRoles.get(0).getRoleId());
        }
        return user;
    }

    @Override
    public User getUserByLoginNameAndPassword(String loginName, String password) {
        return null;
    }

    @Override
    public List<User> getUsers() {
        //return userMapper.selectList(null);
        Page<User> pageParam = new Page<User>(1, 10);
        //IPage<User> userIPage = userMapper.getUsersByCurrentUserId(userVo.getCurrentUserId());
        IPage<User> userIPage = userMapper.getUsersByCurrentUserId(pageParam, ShiroUtil.getCurrentUserId());
        return userIPage.getRecords();
    }


    @Override
    public IPage<User> getUsersPage(Page<User> pageParam) {
//        QueryWrapper<User> wr = new QueryWrapper<User>();
//        wr.eq("create_user", ShiroUtil.getCurrentUserId());
//        return userMapper.selectPage(pageParam, wr);
        IPage<User> userIPage = userMapper.getUsersByCurrentUserId(pageParam, ShiroUtil.getCurrentUserId());
        return userIPage;
    }

    @Override
    public ResponseResult saveUser(User user) {
        user.setPassword(SecurityUtil.encryptPassword("123456", Constants.MD5));
        user.setCreateTime(new Date());
        user.setCreateUser(ShiroUtil.getCurrentUserId());
        user.setUpdateTime(new Date());
        user.setUpdateUser(ShiroUtil.getCurrentUserId());
        Integer flag = userMapper.insert(user);
        if (flag > 0) {
            UserRole userRole = new UserRole();
            userRole.setRoleId(user.getRole());
            userRole.setUserId(user.getId());
            userRoleMapper.insert(userRole);
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult update(User user) {
        user.setUpdateTime(new Date());
        user.setUpdateUser(ShiroUtil.getCurrentUserId());
        Integer flag = userMapper.updateById(user);
        if (flag > 0) {
            QueryWrapper<UserRole> queryWrapper = new QueryWrapper<UserRole>();
            queryWrapper.eq("user_id", user.getId());
            userRoleMapper.delete(queryWrapper);
            UserRole userRole = new UserRole();
            userRole.setRoleId(user.getRole());
            userRole.setUserId(user.getId());
            userRoleMapper.insert(userRole);
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult delete(String ids) {
        if(StringUtils.isNotEmpty(ids)){
            List<String> idList = Arrays.asList(ids.split(","));
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
            userQueryWrapper.in("id", idList);
            int flag = userMapper.delete(userQueryWrapper);
            if(flag > 0 && flag == idList.size()){
                QueryWrapper<UserRole> userRoleWrapper = new QueryWrapper<UserRole>();
                userRoleWrapper.in("user_id", idList);
                userRoleMapper.delete(userRoleWrapper);
                return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public Set<String> getRoleByUserId(Long userId) {
        Set<String> roldIds = new HashSet<String>();
        QueryWrapper<UserRole> userRoleWrapper = new QueryWrapper<UserRole>();
        userRoleWrapper.eq("user_id", userId);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleWrapper);
        if(!CollectionUtils.isEmpty(userRoles)){
            for(UserRole userRole : userRoles){
                roldIds.add(userRole.getRoleId().toString());
            }
        }
        return roldIds;
    }
}
